package com.boss.ccs.pam;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;

import com.boss.core.Boss;
import com.boss.core.BossConstants;
import com.boss.core.BossDescriptor;
import com.boss.core.form.BossFormHelper;
import com.boss.core.util.BossGlobalUtil;
import com.boss.core.util.BossUtil;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import hudson.Extension;
import hudson.util.ListBoxModel;

import com.boss.Messages;

/**
 * This class supports the PAM build order view
 * @author Momin Noor Khan
 */

@Extension
public class BossPAM extends Boss {
    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl) super.getDescriptor();
    }

    /**
     * Gets a brief description about this form (if any)
     */
    @Override
    public String getDescription() {
        return "";
    }

    /**
     * Get the project name from the descriptor
     * @return
     */
    public String getProjectName() {
        return getDescriptor().getProjectName();
    }

    /**
     * Gets the label name to be used when a link for this form is displayed on the post submit landing page
     */
    public String getLabel() {
        return Messages.Boss_PAMFormLabel();
    }

    /**
     * Gets the parent tab of this form
     */
    public String getTabView() {
        return BossConstants.TAB_PAM;
    }

    /**
     * Default action performed on pressing the submit button on the form.
     * @param req
     * @throws ServletException
     * @throws IOException
     */
    public void doSubmit(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
        JSONObject jsonForm = req.getSubmittedForm();
        String project = getProjectName();
        BossPAMForm form = new BossPAMForm(req, jsonForm, project);

        if (form.getJsonTaskSection() == null || StringUtils.isBlank(form.getTask())) {
            BossFormHelper.setFailureMsg(req, Messages.Boss_MissingTaskSection());
            req.getView(this, getPostSubmitView()).forward(req, rsp);
            return;
        }

        if (form.isValid()) {
            form.submit();
        }
        //Display the post-submit landing page
        req.getView(this, getPostSubmitView()).forward(req, rsp);
    }

    /**
     * DescriptorImpl for the PAM Build Order form.
     */
    @Extension
    public static final class DescriptorImpl extends BossDescriptor {
        private String project = BossConstants.PROJECT_PAM;

        /**
         * Get the project name
         * @return
         */
        public String getProjectName(){
            return project;
        }

        /**
         * Populates the module name drop down on the form on the basis of the project (here it is PAM)
         * @return
         */
        public ListBoxModel doFillModuleNameItems() {
            return getListBoxModel(project, BossConstants.MODULES_KEY);
        }

        /**
         * Populates the version number drop down on the form
         * from the properties file on the basis of the selected module
         * @param moduleName
         * @return
         */
        public ListBoxModel doFillVersionNumberItems(@QueryParameter(BossConstants.KEY_MODULE_NAME) String moduleName) {
            ListBoxModel model = new ListBoxModel();
            String svnURL = BossGlobalUtil.getPropertyValue(BossConstants.PROJECT_PAM, BossConstants.SVN_STAGING_URL);
            String svnPath = BossGlobalUtil.getPropertyValue(BossConstants.PROJECT_PAM, BossConstants.SVN_STAGING_PATH)
                    + "/" + moduleName;
            String svnUserName = BossGlobalUtil.getSvnUserName();
            String svnPassword = BossGlobalUtil.getSvnPassword();

            if(!StringUtils.isBlank(moduleName) && !moduleName.equals(BossConstants.DUMMY_VALUE)){
                model = getSVNDirListBoxModel(svnURL, svnPath, svnUserName, svnPassword, true);
            }
            return model;
        }
    }

}
